using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Pokemon
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class SoulManager : DrawableGameComponent, ITappedInner
    {
        List<Soul> _removeableSoul = new List<Soul>();
        
        List<Soul> _addableSoul = new List<Soul>();
        
        List<Soul> _souls = new List<Soul>(); 

        public SoulManager(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        public void Add(Soul s)
        {
            _addableSoul.Add(s);
        }

        public void Remove(Soul s)
        {
            _removeableSoul.Add(s);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            AutoAdd();
            AutoRemove();

            foreach (var soul in _souls)
            {
                soul.Update(gameTime);
            }

            AutoRemove();
            AutoAdd();

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (var soul in _souls)
            {
                soul.Draw(gameTime);
            }
            base.Draw(gameTime);
        }

        private void AutoAdd()
        {
            foreach (var soul in _addableSoul)
            {
                _souls.Add(soul);
            }
            _addableSoul.Clear();
        }

        private void AutoRemove()
        {
            foreach (var soul in _removeableSoul)
            {
                _souls.Remove(soul);
            }
            _removeableSoul.Clear();
        }

        public bool CheckTapped(Point p)
        {
            foreach (var soul in _souls)
            {
                bool isTap = soul.CheckTapped(p);
                if (isTap)
                    return true;
            }
            return false;
        }
    }
}
